package com.cf.demo.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author Chengfei
 * @Description
 * @date 2023/8/7
 */
@Component
public class SpringUtils implements ApplicationContextAware {
    private static ApplicationContext app = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        setApp(applicationContext);
    }


    public static ApplicationContext getApp() {
        return app;
    }

    public static void setApp(ApplicationContext app) {
        SpringUtils.app = app;
    }

    public static Object getBean(String name) {
        return getApp().getBean(name);
    }

    public static <T>T getBean(Class<T> clazz) {
        return getApp().getBean(clazz);
    }

    public static <T>T getBean(String name,Class<T> clazz) {
        return getApp().getBean(name,clazz);
    }

}
